package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.tandong.bottomview.view.BottomView;
import com.tj.jinzao.R;
import com.tj.jinzao.activity.ArticleDetailActivity;
import com.tj.jinzao.adapter.MyMessageTypeListItemAdapter;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.model.DeleteMessage;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.MessagePresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.EmptyView;

import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 我的消息
 * Created by wjy on 2019/1/1.
 */
public class MyMessageTypeFragment extends BaseDataListFragment<List<Article>, MessagePresenter, Article> implements IBaseView<List<Article>> {

    public static MyMessageTypeFragment newInstance(int type) {
        MyMessageTypeFragment fragment = new MyMessageTypeFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(type));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.base_fragment_item_layout);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }

    @Override
    protected MessagePresenter createPresenter() {
        user = UserManager.getInstance().getUser();
        return new MessagePresenter(this);
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
    }
    private  MyMessageTypeListItemAdapter myMessageTypeListItemAdapter;

    @Override
    protected RecyclerView.Adapter createAdapter() {
        myMessageTypeListItemAdapter=new MyMessageTypeListItemAdapter(mContext, mData,mTag);
        return myMessageTypeListItemAdapter;
    }
    private User user;

    @Override
    protected void requestData() {
        if (TextUtils.equals(mTag, "1")) {
            HashMap<String,Object> hashMap=new HashMap<>();
            hashMap.put("currentPage",mPageNo);
            hashMap.put("uid",user.getId());
            mPresenter.getWMAboutMe(hashMap);

        } else {
            HashMap<String,Object> hashMap=new HashMap<>();
            hashMap.put("currentPage",mPageNo);
            hashMap.put("uid",user.getId());
            hashMap.put("type","1,2");
            mPresenter.getReplyMessageList(hashMap);
        }

    }

    @Override
    protected void setEmptyDataView() {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyView.setState(EmptyView.State.NO_DATA, "没有内容哦");
    }

    @Override
    public void onRequestSuccess(List<Article> messages) {
        super.onRequestSuccess(messages);
        if (mPageNo <= 1) {
            mData.clear();
        }
        mData.addAll(messages);
        showListView();

    }

    @Override
    protected boolean resolveData(List<Article> messages) {
        return false;
    }


    @Override
    protected void initData() {
        myMessageTypeListItemAdapter.setOnLongClickLinstener(new MyMessageTypeListItemAdapter.OnLongClickLinstener() {
            @Override
            public void onLongClick(Long id) {
                showPopWindowImage(id);
            }
        });
    }
    private BottomView bottomView;
    private void showPopWindowImage(Long id){
        bottomView=new BottomView(getActivity(), com.netease.nim.uikit.R.style.BottomViewTheme_Defalut, com.netease.nim.uikit.R.layout.item_select);
        bottomView.setAnimation(com.netease.nim.uikit.R.style.myStyle);
        bottomView.dismissBottomView();
        bottomView.showBottomView(false);
        TextView txt_1= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_1);
        TextView txt_2= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_2);
        View view_line=bottomView.getView().findViewById(com.netease.nim.uikit.R.id.view_line);
        view_line.setVisibility(View.GONE);
        txt_1.setText("删除消息");
        txt_2.setVisibility(View.GONE);
        TextView txt_exit= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_exit);
        txt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteMessage(id);
                bottomView.dismissBottomView();

            }
        });
        txt_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomView.dismissBottomView();
            }
        });

    }
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private CompositeSubscription mCompositeSubscription;


    public void detachView() {
        onUnsubscribe();
    }
    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }
    private void deleteMessage(Long id){
        addSubscription(mApiService.deleteMessage(id.intValue()), new SubscriberCallBack<DeleteMessage>(getActivity()) {
            @Override
            protected void onSuccess(DeleteMessage list) {
                ToastUtils.showToast(getActivity(),"删除成功");
                requestData();
            }

            @Override
            protected void onFailure(ResultResponse response) {

            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }

            @Override
            protected void onPageSuccess(int currentPage, int allPage, int allSize) {
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        detachView();
    }

    @Override
    protected void itemClicked(int position, Article data) {
        if(data==null){
            ToastUtils.showToast(getActivity(),"信息错误，请退出重试");
            return;
        }
        if(data.getType()==1||data.getType()==2){
            startActivity(new Intent(mContext, ArticleDetailActivity.class).putExtra("article", data));
        }else {
            if(data.getCircleType()!=0){
                data.setType(data.getCircleType());
                startActivity(new Intent(mContext, ArticleDetailActivity.class).putExtra("article", data));
            }else {
                ToastUtils.showToast(getActivity(),"信息错误，请退出重试");
            }

        }

    }

}
